
package com.tianqu.tms.www.common.action;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ibm.icu.impl.StringUCharacterIterator;
import com.tianqu.tms.core.taglib.MenuBean;
import com.tianqu.tms.core.util.MenuTagUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.www.common.controller.MenuController;

import java.awt.*;
import java.util.List;


/**
 * 登录后迁移到menu
 *
 * @author liugang
 */
public class MenuAction extends TmsAction {

    private Logger logger = Logger.getLogger(this.getClass());

    private static String WWW_DEFAULT_FUNC_ID = "T000";
    public static final String TMSINFO_MESSAGE = "TmsInfoMessage";

    /**
     * @param mapping
     * @param form
     * @param request
     * @param responnse
     * @param uc
     * @return ActionForward
     * @throws Exception
     */
    protected ActionForward doAction(ActionMapping mapping,
                                     ActionForm form, HttpServletRequest request,
                                     HttpServletResponse responnse, UserContext uc) throws Exception {
        logger.debug(this.getClass().getName() + "#doAction was called.");

        String funcId = request.getParameter("funcId");

        String menu = "TMSHome";
        if (funcId == null) {
            funcId = WWW_DEFAULT_FUNC_ID;
        }
        if (!funcId.equals(WWW_DEFAULT_FUNC_ID)) {
            menu = "Menu" + funcId;
        }
        request.setAttribute("funcId", funcId);

        uc.clear();

        MenuController controller = new MenuController();
        controller.execute(uc);
        if (StringUtil.isNotBlank(funcId) && !"T000".equals(funcId)) {
            MenuTagUtil.setMenu(request, funcId);
            List<MenuBean> list = (List<MenuBean>)request.getSession().getAttribute("rightMenuList");
            if(list != null && list.size() > 0){
                String uri = list.get(0).getTargetUri();
                if(StringUtil.isNotBlank(uri)){
                    ActionForward forward = new ActionForward();
                    forward.setPath("/"+uri);
                    return forward;
                }
            }
        }

        request.setAttribute(TMSINFO_MESSAGE, uc.getAttribute(TMSINFO_MESSAGE));
        logger.debug(this.getClass().getName() + "#doAction was completed.");
        return mapping.findForward(menu);
    }

    /**
     *
     */
    protected String getFuncId(HttpServletRequest request, ActionMapping mapping, TmsUserProfile userProfile) {

        String funcId = request.getParameter("funcId");
        if (funcId == null) {
            funcId = WWW_DEFAULT_FUNC_ID;
        }
        return funcId;
    }
}
